package com.niit.broadcastreciver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class systemBroadcast extends AppCompatActivity {
    private systemReceiver systemReceiver;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        systemReceiver = new systemReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        filter.addAction(Intent.ACTION_USER_PRESENT);
        registerReceiver(systemReceiver,filter);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (systemReceiver!=null){
            unregisterReceiver(systemReceiver);
        }
    }

    public class systemReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            switch (intent.getAction()){
                case Intent.ACTION_BOOT_COMPLETED:
                    Log.e("TAG","手机开机了");
                    break;
                case  Intent.ACTION_SHUTDOWN:
                    Log.e("TAG","手机关机了");
                    break;
                case Intent.ACTION_SCREEN_ON:
                    Log.e("TAG","亮屏");
                    Toast.makeText(context,"您收到的广播为：您的手机亮屏啦，可以开始使用啦！",Toast.LENGTH_LONG).show();
                    break;
                case Intent.ACTION_SCREEN_OFF:
                    Log.e("TAG","息屏");

                    break;
                case Intent.ACTION_USER_PRESENT:
                    Log.e("TAG","手机解锁");
                    break;
            }

        }
    }
}
